﻿using System.Web;
using System.Web.Script.Serialization;

namespace NMA.Web.Core.Extensions
{
    public class ErrorResponsor
    {
        public void WriteErrorResponse(string errorMessage)
        {
            var error = new { Message = errorMessage, MessageFormated = false };
            ResponseError(error);
        }

        public void WriteErrorResponseToClient(string errorMessage)
        {
            var error = new { Message = errorMessage, MessageFormatted = true };
            ResponseError(error);
        }

        private void ResponseError(object error)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string Result = serializer.Serialize(error);
            HttpResponse Response = HttpContext.Current.Response;
            Response.ContentType = "application/json";

            Response.TrySkipIisCustomErrors = true;
            Response.StatusCode = 500;
            Response.Write(Result);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
}